package Util::UserTuanStores::UserTuanStores;
use Moose;
use namespace::autoclean;
use JSON;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub search {
	my ($self, $user_id, $tuan_id) = @_;
	my @array = $self->schema->resultset('UserTuanStore')->search({tuan_id => $tuan_id, user_id=> $user_id});
	my %hash;
	foreach (@array) {
		$hash{$_->stores_id} = $_->amount;
	}
	return \%hash;
}

sub get_user_buy_details{
	my ($self, $tuan_id) = @_;
	
	my @stores = $self->schema->resultset('TuanStore')->search(
		{'me.tuan_id' => $tuan_id,
		}
		);
        my %bianhao;
        my $count = 1;
        foreach(@stores) {
                $bianhao{$_->stores_id} = $count;
                $count ++;
        }

	my $tuan = $self->schema->resultset('Tuan')->find($tuan_id);
	my @array = $self->schema->resultset('UserTuanStore')->search({tuan_id => $tuan_id});
	my %buyer_details;
	foreach (@array) {
		my $bianh = $bianhao{$_->stores_id};
		my $amount = $_->amount;
		my $user_id = $_->user_id;
		if(exists $buyer_details{$user_id}) {
			my $tmp = $buyer_details{$user_id};
			my %tmp_hash = %$tmp;
			if(exists $tmp_hash{$bianh}) {
				$tmp->{$bianh} = $tmp->{$bianh} + 1;
			} else {
				$tmp->{$bianh} = $amount;
			}
		} else {
			my %tmp;
			$tmp{$bianh} = $amount;
			$buyer_details{$user_id} = \%tmp;
		}
	}
	return \%buyer_details;
}

sub search_user_tuan_store_by_id {
	my ($self, $user_id, $tuan_id) = @_;
	return $self->schema->resultset('UserTuanStore')->search({tuan_id => $tuan_id, user_id=> $user_id});
}


sub get_user_count {
	my ($self, $tuan_id) = @_;
	my @array = $self->schema->resultset('UserTuanStore')->search(
		{tuan_id => $tuan_id},
                {        
			select => [
				'stores_id',
				{count => 'user_id', -as=> 'user_count'}
			],
			group_by => [qw/stores_id/]
		});
	my %hash;
	foreach (@array) {
		$hash{$_->stores_id} = $_->get_column('user_count');
	}
	return \%hash;
}

sub get_chengyuan{
	my ($self, $tuan_id) = @_;
	my @array = $self->schema->resultset('User')->search(
		{'user_tuan_stores.tuan_id' => $tuan_id},
                {        
			join => 'user_tuan_stores',
			distinct => 'me.id'
		});
	return \@array;
}

sub get_amount {
	my ($self, $tuan_id) = @_;
	my @array = $self->schema->resultset('UserTuanStore')->search(
		{tuan_id => $tuan_id},
                {        
			select => [
				'stores_id',
				{sum => 'amount', -as=> 'buy_amount'}
			],
			group_by => [qw/stores_id/]
		});
	my %hash;
	foreach (@array) {
		$hash{$_->stores_id} = $_->get_column('buy_amount');
	}
	return \%hash;
}

sub create {
	my ($self, $user_id, $tuan_id, $storeAmount) = @_;
	foreach(keys(%$storeAmount)) {
		my $amount = $storeAmount->{$_};
		my $userTuanStore = $self->schema->resultset('UserTuanStore')->create({
			tuan_id => $tuan_id, 
			user_id=> $user_id,
			stores_id => $_,
			amount => $amount
		});
	}
	return 0;
}
sub update {
	my ($self, $user_id, $tuan_id, $storeAmount) = @_;
	foreach(keys(%$storeAmount)) {
		my $amount = $storeAmount->{$_};
		my $userTuanStore = $self->schema->resultset('UserTuanStore')->search({
			tuan_id => $tuan_id, 
			user_id=> $user_id,
			stores_id => $_
		});
		$userTuanStore->update({amount => $amount});
	}
	return 0;
}
sub delete {
	my ($self, $user_id, $tuan_id, $storeAmount) = @_;
	foreach(keys(%$storeAmount)) {
		my $amount = $storeAmount->{$_};
		my $userTuanStore = $self->schema->resultset('UserTuanStore')->search({
			tuan_id => $tuan_id, 
			user_id=> $user_id,
			stores_id => $_
		});
		$userTuanStore->delete;
	}
	return 0;
}

__PACKAGE__->meta->make_immutable;

1;
